package org.hashdb.client.console.decorator;

/**
 * Date: 2024/1/11 3:42
 *
 * @author huanyuMake-pecdle
 * @version 0.0.1
 */
public class StringDecorator {
    private final String source;

    private FontColor fontColor = FontColor.black;

    private BackgroundColor backgroundColor = BackgroundColor.transparent;

    private Typeface typeface = Typeface.normal;

    StringDecorator(String source) {
        this.source = source;
    }

    public static StringDecorator d(String source) {
        return new StringDecorator(source);
    }

    public FontColorSetter font() {
        return new FontColorSetter();
    }

    public BackgroundColorSetter background() {
        return new BackgroundColorSetter();
    }

    public TypefaceSetter typeface() {
        return new TypefaceSetter();
    }

    public String apply() {
        return String.format("\033[%d;%d;%dm%s\033[0m", fontColor.code, backgroundColor.code, typeface.code, source);
    }

    @Override
    public String toString() {
        return apply();
    }

    public class FontColorSetter {
        public StringDecorator black() {
            fontColor = FontColor.black;
            return StringDecorator.this;
        }

        public StringDecorator red() {
            fontColor = FontColor.red;
            return StringDecorator.this;
        }

        public StringDecorator green() {
            fontColor = FontColor.green;
            return StringDecorator.this;
        }

        public StringDecorator yellow() {
            fontColor = FontColor.yellow;
            return StringDecorator.this;
        }

        public StringDecorator blue() {
            fontColor = FontColor.blue;
            return StringDecorator.this;
        }

        public StringDecorator purple() {
            fontColor = FontColor.purple;
            return StringDecorator.this;
        }

        public StringDecorator cyan() {
            fontColor = FontColor.cyan;
            return StringDecorator.this;
        }

        public StringDecorator grey() {
            fontColor = FontColor.grey;
            return StringDecorator.this;
        }
    }

    public class BackgroundColorSetter {
        public StringDecorator transparent() {
            backgroundColor = BackgroundColor.transparent;
            return StringDecorator.this;
        }

        public StringDecorator black() {
            backgroundColor = BackgroundColor.black;
            return StringDecorator.this;
        }

        public StringDecorator red() {
            backgroundColor = BackgroundColor.red;
            return StringDecorator.this;
        }

        public StringDecorator green() {
            backgroundColor = BackgroundColor.green;
            return StringDecorator.this;
        }

        public StringDecorator yellow() {
            backgroundColor = BackgroundColor.yellow;
            return StringDecorator.this;
        }

        public StringDecorator blue() {
            backgroundColor = BackgroundColor.blue;
            return StringDecorator.this;
        }

        public StringDecorator purple() {
            backgroundColor = BackgroundColor.purple;
            return StringDecorator.this;
        }

        public StringDecorator cyan() {
            backgroundColor = BackgroundColor.cyan;
            return StringDecorator.this;
        }

        public StringDecorator grey() {
            backgroundColor = BackgroundColor.grey;
            return StringDecorator.this;
        }
    }

    public class TypefaceSetter {
        public StringDecorator normal() {
            typeface = Typeface.normal;
            return StringDecorator.this;
        }

        public StringDecorator bold() {
            typeface = Typeface.bold;
            return StringDecorator.this;
        }

        public StringDecorator italic() {
            typeface = Typeface.italic;
            return StringDecorator.this;
        }

        public StringDecorator underline() {
            typeface = Typeface.underline;
            return StringDecorator.this;
        }
    }

    public static String red(String s) {
        return "\033[31m" + s + "\033[0m";
    }

    public static String green(String s) {
        return "\033[32m" + s + "\033[0m";
    }

    public static String yellow(String s) {
        return "\033[33m" + s + "\033[0m";
    }

    public static String blue(String s) {
        return "\033[34m" + s + "\033[0m";
    }

    public static String purple(String s) {
        return "\033[35m" + s + "\033[0m";
    }


    /**
     * n. 蓝绿色，青色（用于印刷）
     * adj. 青色的
     */
    public static String cyan(String s) {
        return "\033[36m" + s + "\033[0m";
    }

    public static String underline(String s) {
        return "\033[29;4m" + s + "\033[0m";
    }
}
